package com.lucky.auth_boot.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import com.lucky.auth_boot.common.Result;
import com.lucky.auth_boot.pojo.dto.UserLoginDTO;
import com.lucky.auth_boot.pojo.po.UserPO;
import com.lucky.auth_boot.service.UserService;
import com.lucky.auth_boot.pojo.vo.UserLoginToken;
import com.lucky.auth_boot.utils.RedisUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import static net.sf.jsqlparser.util.validation.metadata.NamedObject.user;

@RestController
@RequestMapping("/admin")
public class LoginController {

    @Resource
    UserService userService;

    @Resource
    RedisUtil redisUtil;

    @PostMapping("/login")
    public Result<UserLoginToken> postUserLogin(@RequestBody UserLoginDTO user) {
        return Result.success(userService.queryUserLogin(user));
    }

    @GetMapping("/code")
    public Result<?> getCode() {
        CircleCaptcha circleCaptcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 5);
        String code = circleCaptcha.getCode();
        String uuid = UUID.randomUUID().toString();;
        redisUtil.setValueWithExpiry("VerifyCode:"+uuid, code, 30,  TimeUnit.SECONDS);

        HashMap<String, Object> data = new HashMap<>();
        data.put("uuid", uuid);
        data.put("img", circleCaptcha.getImageBase64Data());
        return Result.success(data);
    }
}
